/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;

import model.Container;
import model.Player;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class PageLayout extends JLayeredPane {

	Map<Integer, ContainerBox> containerBoxes;
	Player player = Player.getInstance();

	public Collection<ContainerBox> getContainerBoxes() {
		return containerBoxes.values();
	}

	public ContainerBox getContainerBox(int i) {
		Integer id = new Integer(i);
		ContainerBox cb = containerBoxes.get(id);
		if (cb == null) {
			for (Integer integer : containerBoxes.keySet()) {
				// System.out.println(containerBoxes.get(integer).getId());
				// this container does not exist...
			}
		}
		return cb;
	}

	public void init() {
		// this.setIgnoreRepaint(true);
		containerBoxes = new HashMap<Integer, ContainerBox>();
		/*
		 * for (PlaylistRecord record : player.getRecords()) { Container c = new
		 * Container(record.getContainerId()); ContainerBox cb = new
		 * ContainerBox(c); if (containerBoxes.containsKey(cb.getId())) {
		 * continue; } this.containerBoxes.put(Integer.valueOf(c.getId()), cb);
		 * PageLayout.this.add(cb); PageLayout.this.setLayer(cb, c.getDepth());
		 * 
		 * }
		 */
		for (Container c : player.getContainers()) {
			ContainerBox cb = new ContainerBox(c);
			if (containerBoxes.containsKey(cb.getId())) {
				continue;
			}
			this.containerBoxes.put(Integer.valueOf(c.getId()), cb);
			PageLayout.this.add(cb);
			PageLayout.this.setLayer(cb, c.getDepth());

		}
	}

	// when update happens, use this to update containers immediately
	public void updateContainers() {
		/*
		 * this.removeAll(); this.revalidate(); this.repaint(); init(); return;
		 */

		// put container box id in a key set
		Set<Integer> containerKeys = new HashSet<Integer>();
		for (Container c : Player.getInstance().getContainers()) {
			containerKeys.add(new Integer(c.getId()));
		}

		Set<ContainerBox> cbs = new HashSet<ContainerBox>();
		for (Integer containerId : containerBoxes.keySet()) {
			if (!containerKeys.contains(containerId)) {
				cbs.add(containerBoxes.get(containerId));
				// this.remove(cb);
				// System.out.println("Removing container box " + cb.getName());
			}
		}
		for (ContainerBox cb : cbs) {
			containerBoxes.remove(new Integer(cb.getId()));
			this.remove(cb);
		}

		System.out.println(containerBoxes.size() + " boxes left");

		for (final Container c : player.getContainers()) {
			// final Container c = new Container(record.getContainerId());
			ContainerBox cb = new ContainerBox(c);
			// System.out.println("Update Container " + c.getName());
			if (containerBoxes.containsKey(cb.getId())) {
				final ContainerBox box = containerBoxes.get(cb.getId());
				try {
					SwingUtilities.invokeAndWait(new Runnable() {

						@Override
						public void run() {
							box.setBounds(c.getLeft(), c.getTop(), c.getWidth(), c.getHeight());
							PageLayout.this.setLayer(box, c.getDepth());
						}
					});
				} catch (InterruptedException ex) {
					Logger.getLogger(PageLayout.class.getName()).log(Level.SEVERE, null, ex);
				} catch (InvocationTargetException ex) {
					Logger.getLogger(PageLayout.class.getName()).log(Level.SEVERE, null, ex);
				}

			} else {
				this.containerBoxes.put(Integer.valueOf(c.getId()), cb);
				PageLayout.this.add(cb);
				PageLayout.this.setLayer(cb, c.getDepth());
			}
		}
		this.revalidate();
		this.repaint();
	}

	public static void main(String[] args) {
		PageLayout pLayout = new PageLayout();
		System.out.println(pLayout.containerBoxes.size());
	}

	public PageLayout() {
		this.init();
	}
}
